<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="" prop="wxId">
      <el-input v-model="dataForm.wxId" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="name">
      <el-input v-model="dataForm.name" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="password">
      <el-input v-model="dataForm.password" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="img">
      <el-input v-model="dataForm.img" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="0女，1男，默认0" prop="sex">
      <el-input v-model="dataForm.sex" placeholder="0女，1男，默认0"></el-input>
    </el-form-item>
    <el-form-item label="" prop="phone">
      <el-input v-model="dataForm.phone" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="默认地址" prop="address">
      <el-input v-model="dataForm.address" placeholder="默认地址"></el-input>
    </el-form-item>
    <el-form-item label="0正常，1锁定，2逻辑删除" prop="state">
      <el-input v-model="dataForm.state" placeholder="0正常，1锁定，2逻辑删除"></el-input>
    </el-form-item>
    <el-form-item label=" 0普通用户，1vip用户，2管理员" prop="limitt">
      <el-input v-model="dataForm.limitt" placeholder=" 0普通用户，1vip用户，2管理员"></el-input>
    </el-form-item>
    <el-form-item label="" prop="createAt">
      <el-input v-model="dataForm.createAt" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="用户信息修改时间" prop="updateAt">
      <el-input v-model="dataForm.updateAt" placeholder="用户信息修改时间"></el-input>
    </el-form-item>
    <el-form-item label="用户金额" prop="money">
      <el-input v-model="dataForm.money" placeholder="用户金额"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          wxId: '',
          name: '',
          password: '',
          img: '',
          sex: '',
          phone: '',
          address: '',
          state: '',
          limitt: '',
          createAt: '',
          updateAt: '',
          money: ''
        },
        dataRule: {
          wxId: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          name: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          password: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          img: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          sex: [
            { required: true, message: '0女，1男，默认0不能为空', trigger: 'blur' }
          ],
          phone: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          address: [
            { required: true, message: '默认地址不能为空', trigger: 'blur' }
          ],
          state: [
            { required: true, message: '0正常，1锁定，2逻辑删除不能为空', trigger: 'blur' }
          ],
          limitt: [
            { required: true, message: ' 0普通用户，1vip用户，2管理员不能为空', trigger: 'blur' }
          ],
          createAt: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          updateAt: [
            { required: true, message: '用户信息修改时间不能为空', trigger: 'blur' }
          ],
          money: [
            { required: true, message: '用户金额不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/bbm/user/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.wxId = data.user.wxId
                this.dataForm.name = data.user.name
                this.dataForm.password = data.user.password
                this.dataForm.img = data.user.img
                this.dataForm.sex = data.user.sex
                this.dataForm.phone = data.user.phone
                this.dataForm.address = data.user.address
                this.dataForm.state = data.user.state
                this.dataForm.limitt = data.user.limitt
                this.dataForm.createAt = data.user.createAt
                this.dataForm.updateAt = data.user.updateAt
                this.dataForm.money = data.user.money
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/bbm/user/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'wxId': this.dataForm.wxId,
                'name': this.dataForm.name,
                'password': this.dataForm.password,
                'img': this.dataForm.img,
                'sex': this.dataForm.sex,
                'phone': this.dataForm.phone,
                'address': this.dataForm.address,
                'state': this.dataForm.state,
                'limitt': this.dataForm.limitt,
                'createAt': this.dataForm.createAt,
                'updateAt': this.dataForm.updateAt,
                'money': this.dataForm.money
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
